Minimum number of moves required to make all array elements equal¶
Time: O(N) on average; Space: O(1); medium
Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1.
You may assume the array’s length is at most 10,000.
Example 1:
Input: [1,2,3]
Output: 2
Explanation:
Only two moves are needed (remember each move increments or decrements one element):
[1,2,3] => [2,2,3] => [2,2,2]
[1]:
import random
class Solution1(object):
# Quick select solution
def minMoves(self, nums) -> int:
"""
:type nums: List[int]
:rtype: int
"""
def kthElement(nums, k):
def PartitionAroundPivot(left, right, pivot_idx, nums):
pivot_value = nums[pivot_idx]
new_pivot_idx = left
nums[pivot_idx], nums[right] = nums[right], nums[pivot_idx]
for i in range(left, right):
if nums[i] > pivot_value:
nums[i], nums[new_pivot_idx] = nums[new_pivot_idx], nums[i]
new_pivot_idx += 1
nums[right], nums[new_pivot_idx] = nums[new_pivot_idx], nums[right]
return new_pivot_idx
left, right = 0, len(nums) - 1
while left <= right:
pivot_idx = random.randint(left, right)
new_pivot_idx = PartitionAroundPivot(left, right, pivot_idx, nums)
if new_pivot_idx == k - 1:
return nums[new_pivot_idx]
elif new_pivot_idx > k - 1:
right = new_pivot_idx - 1
else: # new_pivot_idx < k - 1
left = new_pivot_idx + 1
median = kthElement(nums, len(nums)//2 + 1)
return sum(abs(num - median) for num in nums)
[2]:
s = Solution1()
assert s.minMoves([1,2,3]) == 2
[3]:
class Solution2(object):
def minMoves(self, nums) -> int:
"""
:type nums: List[int]
:rtype: int
"""
median = sorted(nums)[len(nums)//2]
return sum(abs(num - median) for num in nums)
[4]:
s = Solution2()
assert s.minMoves([1,2,3]) == 2